<!DOCTYPE html>
<html lang="zh-cn">

	<head>
		<meta charset="UTF-8">
		<title class="title"></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<style type="text/css">
			body,html{
				margin: 0;
				padding: 0;
				width: 100%;
				height: 100%;
			}
			.content {
				background-color: #fff;
				border-radius: 5px;
			}
			.con_head{
				text-align: center;
				height: 35px;
				line-height: 35px;
				box-sizing: border-box;
				border-bottom: 1px solid #ccc;
			}
			.select {
				padding: 20px 10px;
				height: 100%;
				
				
			}
			
			.select .file {
				height: 30px;
				border: none;
				outline: none;
				border:0;
				width:180px;
			}
			.cancel{
				box-sizing: border-box;
				text-align: center;
				position: absolute;
				bottom: 14px;
				left: 110px;
				width: 60px;
				height: 30px;
				line-height: 30px;
				border: 1px solid #e2e2e2;
				border-radius: 5px;
				background-color: #fff;
			}
			.cancel:hover{
				background-color: #ccc;
			}
			.prop_mask{
				width: 100%;
				height: 100%;
				position: fixed;
				top: 0;
				left: 0;
				background-color: rgba(0, 0, 0, 0.7);
				z-index: 3000;
				display: none;
			}
			.prompt{
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%,-50%);
				z-index: 3003;
				color: aliceblue;
			}
		</style>
	</head>

	<body>
		
		<div id="content" class="content">
			<div class="select">
				<input onchange="changepic(this)"
				accept="*"
				id="file" class="file" type="file" />
			</div>
		</div>
		<div class="prop_mask">
			<div class="prompt">正在上传，请稍候...</div>
		</div>
		<script type="text/javascript" src="js/webview.1.5.1.js"></script>
		<script type="text/javascript" src="./js/jquery.js"></script>
		<script type="text/javascript">
			var vue_example = null;//代表vue实例，会在vue页面改变值
			var baseUrl = null;
			document.addEventListener('UniAppJSBridgeReady', function() {
			// 通过如下代码，发送信息给app
			
				
			})
			function clickInput(){
				$("#file").trigger("click");
			}
			//监听上传变化
			function changepic(el){
			  let files = $('#file')[0].files;
			  $.each(files,function(index,value){
			    let formData = new FormData();
			    formData.append("file", value);
				let fileName = value.name;
				uploadFile(formData,fileName);
			  })
			}
			//获取token
			function getToken(){
				let re_token = /[?&]token=([^&]*)/;
				let token = re_token.exec(window.location.search)[1];
				return token;
			}
			//上传文件
			function uploadFile(fileData,fileName){
				console.log(fileData,fileName)
				let regex = /[?&]funcId=([^&]*)/;
				let funcId = regex.exec(window.location.search)[1];
				$('.prop_mask').show();
				$.ajax({
					type:'post',
					url: funcId+'/file/upload',
					data: fileData,
					async: false,
					cache: false,
					contentType: false,
					processData: false,
					headers:{
						Authorization: "Bearer " + getToken(),
					},
					dataType: "json",
					success:function(data){
						let json_str = JSON.stringify(data);
						if(data.code == 200){
							let url = data.data.url;
							if(url.indexOf("//127.0.0.1/") != -1){
							  let strIndex = url.indexOf('/uploadimage');
							  url = url.substr(strIndex);
							}
							uni.postMessage({
								data: {
									name:fileName,
									path:url,
									type:"success"
								}
							});
							
						}else{
							uni.postMessage({
								data: {
									name:fileName,
									errMsg:data.msg,
									type:"fail"
								}
							});
						}
						$('.prop_mask').hide();
					},
					fail:function(err){
						$('.prop_mask').hide();
						alert("上传失败")
					}
				})
			}
			//点击取消
			function clickCancel(){
				uni.postMessage({
					data: {
						type:"cancel"
					}
				});
			}
			
			
		</script>
	</body>

</html>

